﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using FZKJ.DBUtility;
namespace FZKJ.DAL
{
    /// <summary>
    /// 数据访问类:buss_card
    /// </summary>
    public partial class buss_card
    {
        private readonly SQLHelper sqldal = null;
        public buss_card(string connStr = "")
        {
            sqldal = new SQLHelper(connStr);
        }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from buss_card");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.VarChar,36)           };
            parameters[0].Value = id;

            return sqldal.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsCode(string code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from buss_card");
            strSql.Append(" where cardno=@code ");
            SqlParameter[] parameters = {
                new SqlParameter("@code", SqlDbType.VarChar,36)           };
            parameters[0].Value = code;

            return sqldal.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(FZKJ.Model.buss_card model)
        {
            SqlParameter[] parameters = new SqlParameter[13];
            string sql = GetAddStr(model, out parameters);
            int rows = sqldal.ExecuteSql(sql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(FZKJ.Model.buss_card model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update buss_card set ");
            strSql.Append("cardno=@cardno,");
            strSql.Append("cardtype=@cardtype,");
            strSql.Append("activationstate=@activationstate,");
            strSql.Append("option1=@option1,");
            strSql.Append("option2=@option2,");
            strSql.Append("creator=@creator,");
            strSql.Append("createtime=@createtime,");
            strSql.Append("deleter=@deleter,");
            strSql.Append("deltime=@deltime,");
            strSql.Append("activetime=@activetime,");
            strSql.Append("starttime=@starttime,");
            strSql.Append("endtime=@endtime,");
            strSql.Append("password=@password,"); 
            strSql.Append("origin=@origin,");
            strSql.Append("isusestatus=@isusestatus");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@cardno", SqlDbType.VarChar,30),
                    new SqlParameter("@cardtype", SqlDbType.VarChar,20),
                    new SqlParameter("@activationstate", SqlDbType.Int,4),
                    new SqlParameter("@option1", SqlDbType.VarChar,100),
                    new SqlParameter("@option2", SqlDbType.VarChar,100),
                    new SqlParameter("@creator", SqlDbType.Int,4),
                    new SqlParameter("@createtime", SqlDbType.DateTime),
                    new SqlParameter("@deleter", SqlDbType.Int,4),
                    new SqlParameter("@deltime", SqlDbType.DateTime),
                    new SqlParameter("@activetime", SqlDbType.DateTime),
                new SqlParameter("@starttime", SqlDbType.Date,3),
                    new SqlParameter("@endtime", SqlDbType.Date,3),
                new SqlParameter("@password", SqlDbType.NVarChar,50),
                    new SqlParameter("@origin", SqlDbType.NVarChar,50),
                    new SqlParameter("@isusestatus", SqlDbType.Int,4),
                    new SqlParameter("@id", SqlDbType.VarChar,36)};
            parameters[0].Value = model.cardno;
            parameters[1].Value = model.cardtype;
            parameters[2].Value = model.activationstate;
            parameters[3].Value = model.option1;
            parameters[4].Value = model.option2;
            parameters[5].Value = model.creator;
            parameters[6].Value = model.createtime;
            parameters[7].Value = model.deleter;
            parameters[8].Value = model.deltime;
            parameters[9].Value = model.activetime;
            parameters[10].Value = model.starttime;
            parameters[11].Value = model.endtime;
            parameters[12].Value = model.password;
            parameters[13].Value = model.origin;
            parameters[14].Value = model.isusestatus;
            parameters[15].Value = model.id;

            int rows = sqldal.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from buss_card ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.VarChar,36)           };
            parameters[0].Value = id;

            int rows = sqldal.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from buss_card ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = sqldal.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FZKJ.Model.buss_card GetModel(string id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,cardno,cardtype,activationstate,option1,option2,creator,createtime,deleter,deltime,activetime,starttime,endtime,password,origin,isusestatus from buss_card ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.VarChar,36)           };
            parameters[0].Value = id;

            FZKJ.Model.buss_card model = new FZKJ.Model.buss_card();
            DataSet ds = sqldal.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FZKJ.Model.buss_card DataRowToModel(DataRow row)
        {
            FZKJ.Model.buss_card model = new FZKJ.Model.buss_card();
            if (row != null)
            {
                if (row["id"] != null)
                {
                    model.id = row["id"].ToString();
                }
                if (row["cardno"] != null)
                {
                    model.cardno = row["cardno"].ToString();
                }
                if (row["cardtype"] != null)
                {
                    model.cardtype = row["cardtype"].ToString();
                }
                if (row["activationstate"] != null && row["activationstate"].ToString() != "")
                {
                    model.activationstate = int.Parse(row["activationstate"].ToString());
                }
                if (row["option1"] != null)
                {
                    model.option1 = row["option1"].ToString();
                }
                if (row["option2"] != null)
                {
                    model.option2 = row["option2"].ToString();
                }
                if (row["creator"] != null && row["creator"].ToString() != "")
                {
                    model.creator = int.Parse(row["creator"].ToString());
                }
                if (row["createtime"] != null && row["createtime"].ToString() != "")
                {
                    model.createtime = DateTime.Parse(row["createtime"].ToString());
                }
                if (row["deleter"] != null && row["deleter"].ToString() != "")
                {
                    model.deleter = int.Parse(row["deleter"].ToString());
                }
                if (row["deltime"] != null && row["deltime"].ToString() != "")
                {
                    model.deltime = DateTime.Parse(row["deltime"].ToString());
                }
                if (row["activetime"] != null && row["activetime"].ToString() != "")
                {
                    model.activetime = DateTime.Parse(row["activetime"].ToString());
                }
                if (row["starttime"] != null && row["starttime"].ToString() != "")
                {
                    model.starttime = DateTime.Parse(row["starttime"].ToString());
                }
                if (row["endtime"] != null && row["endtime"].ToString() != "")
                {
                    model.endtime = DateTime.Parse(row["endtime"].ToString());
                }
                if (row["password"] != null && row["password"].ToString() != "")
                {
                    model.password = row["password"].ToString();
                }
                if (row["origin"] != null && row["origin"].ToString() != "")
                {
                    model.origin = row["origin"].ToString();
                }
                if (row["isusestatus"] != null && row["isusestatus"].ToString() != "")
                {
                    model.isusestatus = int.Parse(row["isusestatus"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,cardno,cardtype,activationstate,option1,option2,creator,createtime,deleter,deltime,activetime,starttime,endtime,password,origin,isusestatus ");
            strSql.Append(" FROM buss_card ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return sqldal.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,cardno,cardtype,activationstate,option1,option2,creator,createtime,deleter,deltime,activetime,starttime,endtime,password,origin,isusestatus ");
            strSql.Append(" FROM buss_card ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return sqldal.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM buss_card ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = sqldal.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*,D.name,D.mobile,D.remark   from (select A.*,B.label as cardTypes from buss_card A left join(select * from[dbo].[sys_dict] where pid = (select id from[dbo].[sys_dict] where value = 'cardtype')) B on A.cardtype = B.id) T left join [dbo].[buss_rescue] C on T.cardno=C.rescuecard left join[dbo].[buss_members] D on C.memberid = D.id");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            if (endIndex > 0)
            {
                strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            }
            return sqldal.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "buss_card";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return sqldal.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        public string GetAddStr(Model.buss_card model, out SqlParameter[] para)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into buss_card(");
            strSql.Append("id,cardno,cardtype,activationstate,option1,option2,creator,createtime,deleter,deltime,activetime,starttime,endtime,password,origin,isusestatus)");
            strSql.Append(" values (");
            strSql.Append("@id,@cardno,@cardtype,@activationstate,@option1,@option2,@creator,@createtime,@deleter,@deltime,@activetime,@starttime,@endtime,@password,@origin,@isusestatus)");
            SqlParameter[] parameters = {
                new SqlParameter("@id", SqlDbType.VarChar,36),
                new SqlParameter("@cardno", SqlDbType.VarChar,30),
                new SqlParameter("@cardtype", SqlDbType.VarChar,20),
                new SqlParameter("@activationstate", SqlDbType.Int,4),
                new SqlParameter("@option1", SqlDbType.VarChar,100),
                new SqlParameter("@option2", SqlDbType.VarChar,100),
                new SqlParameter("@creator", SqlDbType.Int,4),
                new SqlParameter("@createtime", SqlDbType.DateTime),
                new SqlParameter("@deleter", SqlDbType.Int,4),
                new SqlParameter("@deltime", SqlDbType.DateTime),
                new SqlParameter("@activetime", SqlDbType.DateTime),
                new SqlParameter("@starttime", SqlDbType.Date,3),
                new SqlParameter("@endtime", SqlDbType.Date,3),
                new SqlParameter("@password", SqlDbType.NVarChar,50),
                new SqlParameter("@origin", SqlDbType.NVarChar,50),
                new SqlParameter("@isusestatus", SqlDbType.Int,4)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.cardno;
            parameters[2].Value = model.cardtype;
            parameters[3].Value = model.activationstate;
            parameters[4].Value = model.option1;
            parameters[5].Value = model.option2;
            parameters[6].Value = model.creator;
            parameters[7].Value = model.createtime;
            parameters[8].Value = model.deleter;
            parameters[9].Value = model.deltime;
            parameters[10].Value = model.activetime;
            parameters[11].Value = model.starttime;
            parameters[12].Value = model.endtime;
            parameters[13].Value = model.password;
            parameters[14].Value = model.origin;
            parameters[15].Value = model.isusestatus;
            para = parameters;

            return strSql.ToString();
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(List<Model.buss_card> list)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            foreach (var item in list)
            {
                SqlParameter[] parameters = new SqlParameter[13];
                string sql = GetAddStr(item, out parameters);
                cmdList.Add(new CommandInfo { CommandText = sql, Parameters = parameters });
            }
            int rows = sqldal.ExecuteSqlTran(cmdList);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion  ExtensionMethod
    }
}

